* **************************************************************************** *
* Sierra Leone - Social Signaling and Childhood Immunization                   *
* The Effects of Signals on Timely and Complete Vaccination, Separated by Treatment  *
*         - Variable Sample
* **************************************************************************** *
/*
** Purpose:    Create Table:
			 The Effects of Signals on Timely and Complete Vaccination, Separated by Treatment  *
			- Variable Sample

*/
********************************************************************************
********************************************************************************



* **************************************************************************** *
* 1.) Generate  a data set with the outcomes constructed according to timeliness
* **************************************************************************** *



	use  "${Replicate_SocialSignals_dtaInter}/ListingFollowUp_analysis.dta", clear


	* Drop babies born in December 2017 and January 2018:
	keep if dob < td(01dec2017)


	* ----------------------------------------------------------------------------
	* Create a follow-up indicator for each vaccine based on the age of the child
	*   during the last listing:

	local    VaccineVars = " bcg penta1 penta2 penta3 measles1 "

	foreach      vaccine of local VaccineVars {
		gen     age_`vaccine'_mopup    = .
	}


	replace age_bcg_mopup      = 0            if age_listing !=. & age_listing >= 91
	replace age_bcg_mopup      = 1            if age_listing !=. & age_listing <  91

	replace age_penta1_mopup   = 0            if age_listing !=. & age_listing >= 122
	replace age_penta1_mopup   = 1            if age_listing !=. & age_listing <  122

	replace age_penta2_mopup   = 0            if age_listing !=. & age_listing >= 152
	replace age_penta2_mopup   = 1            if age_listing !=. & age_listing <  152

	replace age_penta3_mopup   = 0            if age_listing !=. & age_listing >= 183
	replace age_penta3_mopup   = 1            if age_listing !=. & age_listing <  183

	replace age_measles1_mopup = 0            if age_listing !=. & age_listing >= 350
	replace age_measles1_mopup = 1            if age_listing !=. & age_listing <  350

	gen     age_num_vaccine_mopup = age_measles1_mopup

	* ------------------------------------------------------------------------------
	* Construct outcomes according to timeliness

	* Save the vacine outcomes in seperate variables that will be used to code
	*      the completeness outcome:
	foreach vaccine in bcg penta1 penta2 penta3 measles1 {
		gen `vaccine'_raw = `vaccine'
	}

	* Code Vaccine 1 outcome
	local a = 91

		foreach var in bcg {
		replace `var' = 0          if `var'==1 & age_`var'>=`a' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`a' & age_pikin!=.
		replace `var' = .          if age_pikin<`a'
	}

	* Code Vaccine 2 outcome
	local b = 122

	foreach var in penta1 {
		replace `var' = 0          if `var'==1 & age_`var'>=`b' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`b' & age_pikin!=.
		replace `var' = .          if age_pikin<`b'

		replace `var' = 0          if bcg ==0 & `var'==1
	}


	* Code Vaccine 3 outcome
	local c = 152

	foreach  var in penta2 {
		replace `var' = 0          if `var'==1 & age_`var'>=`c' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`c' & age_pikin!=.
		replace `var' = .          if age_pikin<`c'

		replace `var' = 0          if penta1 ==0 & `var'==1
	}


	* Code Vaccine 4 outcome
	* 183 days - 6 months

	local d = 183

	foreach  var in penta3 {
		replace `var' = 0          if `var'==1 & age_`var'>=`d' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`d' & age_pikin!=. 
		replace `var' = .          if age_pikin<`d'

		replace `var' = 0          if penta2 ==0 & `var'==1
	}


	* Code Vaccine 5 outcome
	* 350 days - 11.5 months
	local e = 350

	foreach var in measles1 {
		replace `var' = 0          if `var'==1&age_`var'>=`e'&age_`var'!=.
		replace `var' = 0          if `var'==.&age_pikin>=`e'&age_pikin!=.
		replace `var' = .          if age_pikin<`e'

		replace `var' = 0          if penta3 ==0&`var'==1
	}


	* total number of timely vaccines      (num_vaccine)
	gen     num_vaccine = .
	replace num_vaccine = 0          if age_pikin>=350 & bcg==0
	replace num_vaccine = 1          if age_pikin>=350 & bcg==1      & penta1==0
	replace num_vaccine = 2          if age_pikin>=350 & penta1==1   & penta2==0
	replace num_vaccine = 3          if age_pikin>=350 & penta2==1   & penta3==0
	replace num_vaccine = 4          if age_pikin>=350 & penta3==1   & measles1==0
	replace num_vaccine = 5          if age_pikin>=350 & measles1==1

	summ    measles1  num_vaccine


	* ----------------------------------------------------------------------------
	* Demean control variables for this sample:

	replace good_memory = 2          if good_memory ==.
	tab     good_memory, gen(recall)

	tab     arm_anc, gen(anc)


	local   VaccineVars = " bcg penta1 penta2 penta3 measles1 num_vaccine "

	foreach vaccine of local VaccineVars {
		foreach var in  recall1 recall2 distance clinic_pop anc2 anc3 anc4 {
			sum  `var'                                          if `vaccine' != .  & intervention_arm==1, detail
			gen  `var'_`vaccine'_dm    = `var' - `r(mean)'      if `vaccine' != .
		}
	}

	* Label the distance variable:
	local     VaccineVars = " bcg penta1 penta2 penta3 measles1 num_vaccine"
	foreach   vaccine of local VaccineVars {
	label var distance_`vaccine'_dm  "Distance"
	}


	summ   bcg penta1 penta2 penta3 measles1 num_vaccine


	gen    panel = 1



	* Save the timely constructed data:
	save  "${Replicate_SocialSignals_dtaFinal}/ListingFollowUp_timely_analysis.dta", replace


	* **************************************************************************** *
	* 2.) Generate  a data set with the outcomes constructed according to by Age One Year
	* **************************************************************************** *


	use  "${Replicate_SocialSignals_dtaInter}/ListingFollowUp_analysis.dta", clear


	* Drop babies born in December 2017 and January 2018:
	keep if dob < td(01dec2017)


	* ----------------------------------------------------------------------------
	* Create a follow-up indicator for each vaccine based on the age of the child
	*   during the last listing:

	local       VaccineVars = " vaccine1 vaccine2 vaccine3 vaccine4 vaccine5 "

	foreach      vaccine of local VaccineVars {
		gen     age_`vaccine'_mopup    = .

	}

	replace age_vaccine1_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine1_mopup = 1                if age_listing !=. & age_listing <  365

	replace age_vaccine2_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine2_mopup = 1                if age_listing !=. & age_listing <  365

	replace age_vaccine3_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine3_mopup = 1                if age_listing !=. & age_listing <  365

	replace age_vaccine4_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine4_mopup = 1                if age_listing !=. & age_listing <  365

	replace age_vaccine5_mopup = 0                if age_listing !=. & age_listing >= 365
	replace age_vaccine5_mopup = 1                if age_listing !=. & age_listing <  365

	gen     age_num_vaccine_mopup = age_vaccine5_mopup


	* ------------------------------------------------------------------------------
	* Generate Ouctome Variables and Controls:

	* Save the vacine outcomes in seperate variables that will be used to code
	*      the completeness outcome:
	foreach vaccine in bcg penta1 penta2 penta3 measles1 {
		gen    `vaccine'_raw = `vaccine'
	}


	* Code Vaccine 1 outcome
	local a = 365

	foreach  var in bcg penta1 penta2 penta3 measles1 {
		replace `var' = 0          if `var'==1 & age_`var'>=`a' & age_`var'!=.
		replace `var' = 0          if `var'==. & age_pikin>=`a' & age_pikin!=.
		replace `var' = .          if age_pikin<`a'
	}


	* Generate the cumulative number of vaccines:
	local   VaccineVars = " bcg penta1 penta2 penta3 measles1 "
	egen    vaccines    = rowtotal(`VaccineVars'), m

	foreach vaccine of numlist 1(1)5 {
		gen     vaccine`vaccine' = 0            if age_pikin >= 365
	}
		foreach vaccine of numlist 1(1)5 {
		replace vaccine`vaccine' = 1            if age_pikin >= 365 & vaccines >= `vaccine'
	}

	* Code the outcome variable: number of vaccines
	gen     num_vaccine = vaccines          if age_pikin >= 365



	* --------------------------------------------------------------------------
	* Demean controls:

	tab     arm_anc, gen(anc)

	replace good_memory = 2          if good_memory ==.
	tab     good_memory, gen(recall)


	foreach var in  recall1 recall2 distance clinic_pop anc2 anc3 anc4  {
		sum    `var'  if intervention_arm==1, detail
		gen    `var'_dm    = `var' - `r(mean)'
	}

	local   VaccineVars = " vaccine1 vaccine2 vaccine3 vaccine4 vaccine5 num_vaccine "

	foreach vaccine of local VaccineVars {
	foreach var in  recall1 recall2 distance clinic_pop anc2 anc3 anc4 {
		sum  `var'                                          if `vaccine' != .  & intervention_arm==1, detail
		gen  `var'_`vaccine'_dm    = `var' - `r(mean)'      if `vaccine' != .
	}
	}

	* Label the distance variable:
	local     VaccineVars =  " vaccine1 vaccine2 vaccine3 vaccine4 vaccine5 num_vaccine"
	foreach   vaccine of local VaccineVars {
		label var distance_`vaccine'_dm  "Distance"
	}

	local    VaccineVars =  " vaccine1 vaccine2 vaccine3 vaccine4 vaccine5 num_vaccine"

	summ    `VaccineVars'



	* --------------------------------------------------------------------------

	gen    panel = 2



	* Save the timely constructed data:
	save  "${Replicate_SocialSignals_dtaFinal}/ListingFollowUp_byAgeOneYear_analysis.dta", replace


	* **************************************************************************** *
	* 3.) Append data sets and prepare Panel
	* **************************************************************************** *


	* ******************************************************************************
	* Generate Panel with Controls

	use  "${Replicate_SocialSignals_dtaFinal}/ListingFollowUp_timely_analysis.dta", clear

	append using "${Replicate_SocialSignals_dtaFinal}/ListingFollowUp_byAgeOneYear_analysis.dta"


	label var treat3       "Signal at 4"
	label var treat4       "Signal at 5"
	label var treat2       "Uninformative Bracelet"
	label var distance_dm  "Distance"

	summ      bcg penta1 penta2 penta3 measles1  num_vaccine if panel == 1

	summ      bcg penta1 penta2 penta3 measles1  num_vaccine if panel == 2



	** Panel A: Effects of Signals on Timely Vaccination - Compared to Control:
	eststo clear
	
	foreach vaccine in bcg penta1 penta2 penta3 measles1  num_vaccine  {
		eststo   m_`vaccine'_a:  areg `vaccine' treat3 treat4 treat2  age_`vaccine'_mopup      recall1_`vaccine'_dm recall2_`vaccine'_dm distance_`vaccine'_dm clinic_pop_`vaccine'_dm         anc2_`vaccine'_dm anc3_`vaccine'_dm anc4_`vaccine'_dm       if panel == 1, absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
		test   treat2 = treat4
		estadd scalar treat2_treat4 = r(p)
		test   treat2 = treat3
		estadd scalar treat2_treat3 = r(p)
		test   treat3 = treat4
		estadd scalar treat3_treat4 = r(p)
		test   treat2 treat3 treat4
		estadd scalar bracelets     = r(p)
		estadd scalar C_mean   = _b[_cons]
		estadd local  Obs      = "`e(N)'"
		estadd local  controls = "Yes"
		eststo m_`vaccine'_a_full
	}

	esttab  ///
	m_bcg_a_full    m_penta1_a_full    m_penta2_a_full       ///
	m_penta3_a_full m_measles1_a_full  m_num_vaccine_a_full   ///
	using "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex",     ///
	refcat(treat3 "\hline \multicolumn{1}{l}{\textbf{\textit{Panel A:}}} & \multicolumn{6}{c}{Effects of Signals on Timely Vaccination} \\ \hline %", nolabel) ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule"                  ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{1}{c}{\textbf{1 Vaccine}} & \multicolumn{1}{c}{\textbf{2 Vaccines}} & \multicolumn{1}{c}{\textbf{3 Vaccines}} & \multicolumn{1}{c}{\textbf{4 Vaccines}} & \multicolumn{1}{c}{\textbf{5 Vaccines}} & \multicolumn{1}{c}{\textbf{Total \# of vaccines}} \\")  ///
	keep(treat3 treat4 treat2 )                                           ///
	scalars("C_mean        Control Group mean"                            ///
	"Obs           Observations"                                  ///
	"treat2_treat3 \(S_{4}\) \(>\) 0: p(UI = S4)"                 ///
	"treat2_treat4 \(S_{5}\) \(>\) 0: p(UI = S5)"                 ///
	"treat3_treat4 p(S4 = S5)"                                    ///
	"bracelets     Joint F-Test"                                  ///
	"controls      Controls")                                     ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	sfmt(3) b(3) se(3)  ${StarsOpt}                                       ///
	fragment nogaps nomtitles label noobs nolines                         ///
	replace


	** Panel B: Effects of Signals on Vaccination by Age One Year
	foreach  vaccine in   vaccine1 vaccine2 vaccine3 vaccine4 vaccine5  num_vaccine     {
		eststo   model_`vaccine'_b:  areg `vaccine' treat3 treat4 treat2   age_`vaccine'_mopup   recall1_`vaccine'_dm  recall2_`vaccine'_dm   distance_`vaccine'_dm   clinic_pop_`vaccine'_dm      anc2_`vaccine'_dm anc3_`vaccine'_dm anc4_`vaccine'_dm     if panel == 2, absorb(strata) vce(bootstrap, reps(${RepsNum}) seed(${seed}) cluster(clinic))
		test   treat2 = treat4
		estadd scalar treat2_treat4 = r(p)
		test   treat2 = treat3
		estadd scalar treat2_treat3 = r(p)
		test   treat3 = treat4
		estadd scalar treat3_treat4 = r(p)
		test   treat2 treat3 treat4
		estadd scalar bracelets     = r(p)
		estadd scalar C_mean  = _b[_cons]
		estadd local Obs      = "`e(N)'"
		estadd local controls = "Yes"
		eststo m_`vaccine'_b_full
	}


	esttab  ///
	m_vaccine1_b_full  m_vaccine2_b_full  m_vaccine3_b_full             ///
	m_vaccine4_b_full  m_vaccine5_b_full  m_num_vaccine_b_full          ///
	using "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex",        ///
	refcat(treat3 "\hline \multicolumn{1}{l}{\textbf{\textit{Panel B:}}}  & \multicolumn{6}{c}{Effects of Signals on Vaccination by Age One Year} \\ \hline %", nolabel) ///
	keep(treat3 treat4 treat2 )                                         ///
	scalars("C_mean        Control Group mean"                          ///
	"Obs           Observations"                                ///
	"treat2_treat3 \(S_{4}\) \(>\) 0: p(UI = S4)"               ///
	"treat2_treat4 \(S_{5}\) \(>\) 0: p(UI = S5)"               ///
	"treat3_treat4 p(S4 = S5)"                                  ///
	"bracelets     Joint F-Test"                                ///
	"controls      Controls")                                   ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))              ///
	sfmt(3) b(3) se(3)  ${StarsOpt}                                     ///
	fragment append nomtitles noobs nonumbers nolines                   ///
	postfoot("\bottomrule \end{tabular}")                               ///
	label

	filefilter "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex"

	filefilter "${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_WithControls_VariableSample.tex", from("pvalue\BS_str") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw.tex"





	* ----------------------------------------------------------------------------
	* outsheet p-values, with controls


	esttab  ///
	m_bcg_a_full    m_penta1_a_full    m_penta2_a_full       ///
	m_penta3_a_full m_measles1_a_full  m_num_vaccine_a_full   ///
	using "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex",     ///
	refcat(treat3 "\hline \multicolumn{1}{l}{\textbf{\textit{Panel A:}}} & \multicolumn{6}{c}{Effects of Signals on Timely Vaccination} \\ \hline %", nolabel) ///
	prehead("\begin{tabular}{l*{7}{c}} \toprule"                  ///
	"\multicolumn{1}{l}{\textbf{Dependent variable:}} & \multicolumn{1}{c}{\textbf{1 Vaccine}} & \multicolumn{1}{c}{\textbf{2 Vaccines}} & \multicolumn{1}{c}{\textbf{3 Vaccines}} & \multicolumn{1}{c}{\textbf{4 Vaccines}} & \multicolumn{1}{c}{\textbf{5 Vaccines}} & \multicolumn{1}{c}{\textbf{Total \# of vaccines}} \\")  ///
	keep(treat3 treat4 treat2 )                                           ///
	scalars("C_mean        Control Group mean"                            ///
	"Obs           Observations"                                  ///
	"treat2_treat3 \(S_{4}\) \(>\) 0: p(UI = S4)"                 ///
	"treat2_treat4 \(S_{5}\) \(>\) 0: p(UI = S5)"                 ///
	"treat3_treat4 p(S4 = S5)"                                    ///
	"bracelets     Joint F-Test"                                  ///
	"controls      Controls")                                     ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))                ///
	sfmt(3) b(3) p(3)  ${StarsOpt}                                       ///
	fragment nogaps nomtitles label noobs nolines                         ///
	replace


	esttab  ///
	m_vaccine1_b_full  m_vaccine2_b_full  m_vaccine3_b_full             ///
	m_vaccine4_b_full  m_vaccine5_b_full  m_num_vaccine_b_full          ///
	using "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex",        ///
	refcat(treat3 "\hline \multicolumn{1}{l}{\textbf{\textit{Panel B:}}}  & \multicolumn{6}{c}{Effects of Signals on Vaccination by Age One Year} \\ \hline %", nolabel) ///
	keep(treat3 treat4 treat2 )                                         ///
	scalars("C_mean        Control Group mean"                          ///
	"Obs           Observations"                                ///
	"treat2_treat3 \(S_{4}\) \(>\) 0: p(UI = S4)"               ///
	"treat2_treat4 \(S_{5}\) \(>\) 0: p(UI = S5)"               ///
	"treat3_treat4 p(S4 = S5)"                                  ///
	"bracelets     Joint F-Test"                                ///
	"controls      Controls")                                   ///
	substitute(\(S\_{4}\) \(S_{4}\)  \(S\_{5}\) \(S_{5}\))              ///
	sfmt(3) b(3) p(3)  ${StarsOpt}                                     ///
	fragment append nomtitles noobs nonumbers nolines                   ///
	postfoot("\bottomrule \end{tabular}")                               ///
	label

	filefilter "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex"      ///
	"${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_panel_controls_new.tex"

	filefilter "${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw.tex"      ///
	"${Replicate_SocialSignals_ATables}/pvalues/Table_EffectsOfSignals_Timely_AgeOneYear_WithControls_VariableSample_pvals.tex", from("pvalue\BS_str") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_EffectsOfSignals_Timely_AgeOneYear_raw.tex"



* **************************************************************************** *
*  End of the Dofile !!!
* **************************************************************************** *
